package com.bingo.sds.controller;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bingo.framework.util.ResultTools;
import com.bingo.sds.entity.Order;
import com.bingo.sds.repository.OrderRepository;


@RequestMapping(value="/order")
@Controller
public class OrderController {
	@Autowired
	private OrderRepository repository;
	
	@RequestMapping(value="/saveTest")
	@ResponseBody
	public String saveTest() {
		Long idStart = new Date().getTime();
		for(int i=0; i<5; i++) {
			Order order = new Order();
			order.setOrderId(idStart + i);
			order.setOrderName("order"+order.getOrderId());
			order.setOrderType(new Random().nextLong());
			repository.save(order);
		}
		
		return ResultTools.successResult("5 records saved");
	}
	
	@GetMapping(value="/getById/{orderId}")
	@ResponseBody
	public String getById(@PathVariable Long orderId) {
		if(orderId == null) {
			return ResultTools.errorResult("id cannot be null");
		}
		
		Optional<Order> order = repository.findById(orderId);
		
		if(order.isPresent()) {
			return ResultTools.dataResult(order.get());
		}
		
		return ResultTools.successResult("no data");
	}
	
	@GetMapping(value="/getByType/{orderType}")
	@ResponseBody
	public String getByType(@PathVariable Long orderType) {
		if(orderType == null) {
			return ResultTools.errorResult("orderType cannot be null");
		}
		
		Order orderParam = new Order();
		orderParam.setOrderType(orderType);
		
		Example<Order> ex = Example.of(orderParam);
		List<Order> orderList = repository.findAll(ex);
		
		if(orderList != null && !orderList.isEmpty()) {
			return ResultTools.dataResult(orderList);
		}
		
		return ResultTools.successResult("no data");
	}
	
	@GetMapping(value="/getByTypeAndId/{orderType}/{orderId}")
	@ResponseBody
	public String getByTypeAndId(@PathVariable Long orderType, @PathVariable Long orderId) {
		if(orderType == null) {
			return ResultTools.errorResult("orderType cannot be null");
		}
		if(orderId == null) {
			return ResultTools.errorResult("orderId cannot be null");
		}
		
		Order orderParam = new Order();
		orderParam.setOrderId(orderId);
		orderParam.setOrderType(orderType);
		
		Example<Order> ex = Example.of(orderParam);
		Optional<Order> orderResult = repository.findOne(ex);
		
		if(orderResult.isPresent()) {
			return ResultTools.dataResult(orderResult.get());
		}
		
		return ResultTools.successResult("no data");
	}
	
	@GetMapping(value="/deleteById/{orderId}")
	@ResponseBody
	public String deleteById(@PathVariable Long orderId) {
		if(orderId == null) {
			return ResultTools.errorResult("orderId cannot be null");
		}
		
		try {
			repository.deleteById(orderId);
		}catch (Exception e) {
			e.printStackTrace();
			return ResultTools.errorResult("Error:"+e.getMessage());
		}
		
		return ResultTools.successResult();
	}
	
	@GetMapping(value="/deleteByTypeAndId/{orderType}/{orderId}")
	@ResponseBody
	public String deleteByTypeAndId(@PathVariable Long orderType, @PathVariable Long orderId) {
		if(orderType == null) {
			return ResultTools.errorResult("orderType cannot be null");
		}
		if(orderId == null) {
			return ResultTools.errorResult("orderId cannot be null");
		}
		
		Order orderParam = new Order();
		orderParam.setOrderId(orderId);
		orderParam.setOrderType(orderType);
		
		try {
			repository.delete(orderParam);
		}catch (Exception e) {
			e.printStackTrace();
			return ResultTools.errorResult("Error:"+e.getMessage());
		}
		
		return ResultTools.successResult();
	}
	
	@RequestMapping(value="/saveBatch")
	@ResponseBody
	public String saveOrUpdate(@RequestBody List<Order> paramList) {
		if(paramList == null || paramList.isEmpty()) {
			return ResultTools.errorResult("param error");
		}
		
		try {
			repository.saveAll(paramList);
		}catch (Exception e) {
			e.printStackTrace();
			return ResultTools.errorResult("Error:"+e.getMessage());
		}
		
		return ResultTools.successResult();
	}
	
	@RequestMapping(value="/updateNameById")
	@ResponseBody
	public String updateNameById(@RequestBody Order param) {
		if(param == null || param.getOrderId() == null || param.getOrderName() == null) {
			return ResultTools.errorResult("param error");
		}
		
		Optional<Order> order = repository.findById(param.getOrderId());
		
		if(order.isPresent()) {
			param.setOrderType(order.get().getOrderType());
			try {
				repository.deleteById(param.getOrderId());
				repository.save(param);
			}catch (Exception e) {
				e.printStackTrace();
				return ResultTools.errorResult("Error:"+e.getMessage());
			}
		}else {
			return ResultTools.errorResult("Error:cannot find the record");
		}
		
		return ResultTools.successResult();
	}
}
